import time
from fileinput import filename

from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from common.Utils import BlogDriver


# 测试博客登录页面
class BlogLogin:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    # 成功登录的测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 能找到博客首页用户的昵称,说明登录成功，否则登录失败
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        # 添加屏幕截图
        BlogDriver.getScreenShot()
        time.sleep(2)
        self.driver.back()

    # 异常登陆的测试用例
    def LoginFailTest(self):
        # 若连续多次的send_keys则会出现关键词拼接，而不是替换。若要替换需要先clear
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        # 错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 检查登录失败
        '''
        弹窗直接使用css选择器是拿不到的，需要切换到弹窗
        '''
        # 代码执行太快，但是弹窗还没出来，所以加一个强制等待
        time.sleep(3)
        alter_ = self.driver.switch_to.alert
#        actual = self.driver.find_element(By.CSS_SELECTOR, "body").text
        '''获取弹窗的文本内容'''
        actual = alter_.text
        # 添加屏幕截图
        '''屏幕截图需要重新写一下，当存在弹窗的时候，直接只用selenium的屏幕截图会报错，应该换其其他框架的屏幕截图'''
       # BlogDriver.getScreenShot()
        # 断言一下
        assert actual=="密码错误"
        # 关闭弹窗
        alter_.accept()
    #   self.driver.back()

    # 新增：特殊符号登录测试
    def LoginWithSpecialChars(self):
        # 定义一组特殊符号测试用例
        test_cases = [
            {"username": "!@#$%^&*()", "password": "123456", "desc": "特殊符号用户名"},
            {"username": "zhangsan", "password": "!@#$%^&*()", "desc": "特殊符号密码"},
            {"username": "!@#$%^&*()", "password": "!@#$%^&*()", "desc": "用户名密码均为特殊符号"},
            {"username": "admin'--", "password": "123456", "desc": "SQL注入测试"},
            {"username": "<script>alert(1)</script>", "password": "123456", "desc": "XSS测试"}
        ]

        for case in test_cases:
            print(f"\n正在执行测试用例: {case['desc']}")
            try:
                # 清空输入框
                self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
                self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

                # 输入测试数据
                self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(case["username"])
                self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(case["password"])
                self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

                # 处理可能的弹窗
                time.sleep(2)
                try:
                    alert = self.driver.switch_to.alert
                    alert_text = alert.text
                    print(f"出现弹窗: {alert_text}")
                    alert.accept()

                    # 验证是否为预期的错误提示
                    if "用户名或密码错误" in alert_text or "不合法" in alert_text:
                        print(f"测试通过 - {case['desc']}: 系统正确处理了特殊符号输入")
                    else:
                        print(f"测试警告 - {case['desc']}: 出现未知弹窗: {alert_text}")
                except:
                    # 没有弹窗，检查是否登录成功
                    try:
                        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
                        print(f"测试失败 - {case['desc']}: 系统接受了特殊符号作为有效登录凭证")
                        self.driver.back()
                    except NoSuchElementException:
                        print(f"测试通过 - {case['desc']}: 系统拒绝了特殊符号登录")

                BlogDriver.getScreenShot()
                self.driver.get(self.url)  # 重新加载登录页面

            except Exception as e:
                print(f"测试异常 - {case['desc']}: {str(e)}")
                BlogDriver.getScreenShot()
                self.driver.get(self.url)  # 重新加载登录页面

        print("\n所有特殊符号登录测试完成")


login = BlogLogin()
# zhushi
#login.LoginSucTest()
#login.LoginFailTest()
